// 数据库操作
const mongoose = require('mongoose');
// 模型规则类
const {
    Schema
} = mongoose;

// 对象规则验证
const Joi = require('joi');
// 文章模型规则
const revSchema = new Schema({
    // 订单号
    rev_id: {
        type: String,
        minlength: 5,
        maxlength: 40,
        required: [true, '订单号不能为空'],
        unique: [true, '已存在的订单号']
    },
    // 是否已阅
    isChecked: {
        type: Boolean,
        required: true,
        default: false
    },
    // 订房状态：0-正常; 1-更改; 2-取消
    state: {
        type: Number,
        required: true,
        default: 0,
        min: 0,
        max: 9
    },
    // 订单状态: 0-已确认; 1-未确认; 2-拒单
    status: {
        type: Number,
        required: true,
        default: 0,
        min: 0,
        max: 9
    },
    // 预订用户的id
    customer: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Customer',
        required: true
    },
    // 预订方式
    source: {
        type: String,
        minlength: 2,
        maxlength: 16,
        default: 'web'
    },
    // 预订渠道
    channel: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Channel',
        required: true
    },
    // 预订获取时的渠道语言
    channelLang: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'ChannelLang',
        required: true
    },
    // 已付押金
    deposit: {
        type: String,
        minlength: 1,
        maxlength: 10,
        default: '0.00'
    },
    // 总价
    subtotal: {
        type: String,
        minlength: 1,
        maxlength: 10,
        default: '0.00'
    },
    // 手续费
    commission: {
        type: String,
        minlength: 1,
        maxlength: 10,
        default: '0.00'
    },
    // 预订产生的时间
    bookedAt: {
        type: Date,
        required: true,
        default: '1988-04-16'
    },
    // 到店日期
    arrivalAt: {
        type: Date,
        required: true,
        default: '1988-04-16'
    },
    // 离开日期
    departureAt: {
        type: Date,
        required: true,
        default: '1988-04-16'
    },
    // 到店时间
    arrivalTime: {
        type: String,
        default: null
    },
    // 预订人数
    persons: {
        type: Number,
        required: true,
        default: 0,
        min: 0
    },
    // 预订天数
    nights: {
        type: Number,
        required: true,
        default: 0,
        min: 0
    },
    // 预订房间数
    rooms: {
        type: Number,
        required: true,
        default: 0,
        min: 0
    },
    // 留言要求
    comments: {
        type: String,
        default: null
    },
    // 用户最近一次修改的时间
    lastChangedAt: {
        type: Date,
        default: '1988-04-16'
    },
    // 订单记录时间
    createAt: {
        type: Date,
        required: true,
        default: Date.now
    },
    // 最近一次订单被修改时间
    updateAt: {
        type: Date,
        required: true,
        default: Date.now
    },
    // 取消时间
    cancelAt: {
        type: Date,
        default: null
    }
}, {
    versionKey: false
});

const Rev = mongoose.model('Rev', revSchema);

// 时间更新
revSchema.pre('findOneAndUpdate', function (next) {
    this.findOneAndUpdate({}, {
        updateAt: Date.now()
    })
    next();
});

// 文章格式校验
const validateRev = rev => {
    // 定义对象验证规则
    const schema = {
        /* title: Joi.string().min(2).max(100).required().error(new Error('文章标题不符合验证验证规则')),
        state: Joi.number().valid([0, 1]).default(0, 'draft').error(new Error('文章状态值非法')),
        feature: Joi.any().empty(),
        content: Joi.string(),
        category: Joi.string().regex(/^[0-9a-fA-F]{24}$/).required().error(new Error('分类id格式非法')) */
    };
    // 验证
    return Joi.validate(rev, schema, {
        // 检测到所有错误
        abortEarly: false,
        // 允许对象包含被忽略的未知键
        allowUnknown: true
    });
}

// 导出模块成员
module.exports = {
    Rev,
    validateRev
}